package com.epoch.serviceconsumer.controller;

import bean.response.ResponseBean;
import bean.response.ResultEnum;
import com.epoch.serviceconsumer.bean.Consumer;
import com.epoch.serviceconsumer.service.ConsumerService;
import com.epoch.zuul.login.LoginService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.xss.core.XssCleanIgnore;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.ResultInfoUtil;
import utils.validcode.ValidCodeUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;

/**
 * 进行用户的相关操作
 * @author Seven
 * @date 2021/11/01
 */
@RestController
@Slf4j
public class ConsumerController {

//    @Resource
//    private DefaultKaptcha captchaProducer;
//    /**
//     * 登录验证码SessionKey checkcode
//     * //    public static final String LOGIN_VALIDATE_CODE = "login_validate_code";
//     */
//    public static final String LOGIN_VALIDATE_CODE = "checkcode";

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ConsumerService consumerService;

//    @Autowired
//    private LoginService loginService;

    @ApiOperation(value = "用户版块")
    @GetMapping("epoch/consumer/getConsumerById/{csId}")
    public ResponseBean getConsumerById(@PathVariable("csId") String csId){
        return consumerService.getById(csId);
    }

//    @XssCleanIgnore
//    @ApiOperation(value = "用户版块")
//    @PostMapping("epoch/consumer/login")
//    public ResponseBean login(@Valid @RequestBody Consumer currentConsumer, HttpServletRequest request){
////        , @RequestParam("validateCode") String validateCode
//        String validateCode = "1x1x";
//        return loginService.login(currentConsumer, validateCode, request);
//    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping("epoch/consumer/managerLogin")
//    public ResponseBean managerLogin(@Valid @RequestBody Consumer currentConsumer, HttpServletRequest request){
//        return loginService.managerLogin(currentConsumer, request);
//    }

    @ApiOperation(value = "用户版块")
    @GetMapping("epoch/consumer/logOut")
    public ResponseBean logOut(){
        SecurityUtils.getSubject().logout();
        return ResultInfoUtil.buildSuccess(ResultEnum.OK,"用户退出登录成功");
    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping("epoch/consumer/register")
//    public ResponseBean register(@Valid @RequestBody Consumer regConsumer, HttpServletRequest request, @RequestParam("registerFor") String registerFor,
//                                 @RequestParam("validateCode") String validateCode){ return loginService.register(regConsumer, request, registerFor, validateCode); }

//    @ApiOperation(value = "用户版块")
//    @PutMapping("epoch/consumer/csInfoComplete")
//    public ResponseBean csInfoComplete(@Valid @RequestBody Consumer completeConsumer, HttpServletRequest request){
//        return consumerService.csInfoComplete(completeConsumer, request);
//    }

//    @ApiOperation(value = "用户版块")
//    @RequiresPermissions({"consumer:delete"})
//    @DeleteMapping("epoch/consumer/deleteCsByCsId")
//    public ResponseBean deleteCsByCsId(@RequestParam("csId") String csId){
//        return consumerService.deleteCsByCsId(csId);
//    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping("epoch/consumer/changePwd")
//    public ResponseBean changePwd(@Validated @RequestBody HashMap<String,String> map){
//        return consumerService.changePwd(map);
//    }

//    /**
//     * 登录验证码图片A
//     * @param request
//     * @param response
//     * @throws Exception
//     */
//    @ApiOperation(value = "其他版块")
//    @GetMapping(value = {"/loginValidateCodeA"})
//    public void loginValidateCodeA(HttpServletRequest request, HttpServletResponse response) throws Exception{
//        ValidCodeUtil.validateCode(request, response, captchaProducer, LOGIN_VALIDATE_CODE);
//    }

//    /**
//     * 检查验证码是否正确
//     */
//    @ApiOperation(value = "其他版块")
//    @PostMapping("/checkLoginValidateCodeA")
//    @ResponseBody
//    public HashMap checkLoginValidateCodeA(HttpServletRequest request,@RequestParam("validateCode")String validateCode) {
//        String loginValidateCode = request.getSession().getAttribute("verCode").toString();
//        System.out.println("验证码："+loginValidateCode);
//        HashMap<String,Object> map = new HashMap<String,Object>(10);
//        if(loginValidateCode == null){
//            map.put("status",null);
//            //验证码过期
//        }else if(loginValidateCode.equals(validateCode)){
//            map.put("status",true);
//            //验证码正确
//        }else if(!loginValidateCode.equals(validateCode)){
//            map.put("status",false);
//            //验证码不正确
//        }
//        map.put("code",200);
//        return map;
//    }

//    /**
//     * 获取 图片验证码B
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    @ApiOperation(value = "其他版块")
//    @GetMapping(value="/loginValidateCodeB")
//    public void loginValidateCodeB(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        loginService.loginValidateCodeB(request,response);
//    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping("epoch/consumer/addPhoto")
//    public ResponseBean addPhoto(@RequestParam("multipartFile") MultipartFile multipartFile, @RequestParam("csId") String csId, HttpServletResponse response){
//        return consumerService.addPhoto(multipartFile, csId, response);
//    }

//    @ApiOperation(value = "用户版块")
//    @GetMapping("epoch/consumer/getPhoto/{csId}")
//    public ResponseBean getPhoto(@PathVariable("csId") String csId,HttpServletResponse response){
//        return consumerService.getPhoto(csId, response);
//    }

//    @ApiOperation(value = "用户版块")
//    @GetMapping(value="epoch/consumer/getConsumerList")
//    public ResponseBean getConsumerList(){
//        return consumerService.getConsumerList();
//    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping(value="epoch/consumer/addManager")
//    public ResponseBean addManager(@RequestBody @Valid Consumer consumer){
//        return consumerService.addManager(consumer);
//    }

//    @ApiOperation(value = "用户版块")
//    @PostMapping(value="epoch/consumer/updateCsUseState")
//    public ResponseBean updateCsUseState(@RequestBody HashMap<String,Object> map){
//        return consumerService.updateCsUseState(map);
//    }

}
